package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.page.FindPage;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Author chongge
 * Time 2019/10/31 10:04
 * ClassName com.itheima.service.impl
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private MenuDao menuDao;

    /***
     * 根据用户名查找用户,返回具体数据
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {

        User user = userDao.findUserByUsername(username);
        if (user == null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findRoleByUserId(userId);
        if (roles != null && roles.size() > 0){
            //遍历角色
            for (Role role : roles) {
                //获取角色id
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findPermissionByRoleId(roleId);
                //获取菜单列表
                LinkedHashSet<Menu> menus = menuDao.findMenusByroledId(roleId);
                role.setMenus(menus);
                role.setPermissions(permissions);
            }
            user.setRoles(roles);
        }


        return user;
    }

    @Override
    public List<Menu> getMenus(String username) {

        List<Menu> res = new ArrayList<>();
        User user = findUserByUsername(username);

        for (Role role : user.getRoles()) {

            LinkedHashSet<Menu> menus = role.getMenus();
            for (Menu menu : menus) {

                if (!res.contains(menu)) {
                    res.add(menu);
                }
            }
        }

        Collections.sort(res, Comparator.comparing(Menu::getPriority));

        return res;
    }


    /**
     * @author TanLin
     * 用户分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPagePermission(QueryPageBean queryPageBean) {
        String queryString = FindPage.findPage(queryPageBean);
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Page<User> checkItemPage = userDao.findPageUser(queryString);
        return new PageResult(checkItemPage.getTotal(),checkItemPage.getResult());
    }

    /**
     * 获取所有角色
     * @return
     */
    @Override
    public List<Role> findRoleAll() {
        return userDao.findRoleAll();
    }


    /**
     * 添加用户和角色
     * @param user
     * @param checkitemIds
     */
    @Override
    public void addUser(User user, Integer[] checkitemIds) {
        userDao.addUser(user);
        Integer id = user.getId();
        if (checkitemIds != null && checkitemIds.length >0){
            for (Integer checkitemId : checkitemIds) {
                userDao.addUserRole(id,checkitemId);
            }
        }
    }

    /**
     * 回显用户信息
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }

    /**
     * 查询用户关联得角色集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findUserIdsByRoleId(Integer id) {
        return userDao.findUserIdsByRoleId(id);
    }


    /**
     * 提交编辑数据
     * @param user
     * @param checkitemIds
     */
    @Override
    public void editUser(User user, Integer[] checkitemIds) {
        userDao.editUser(user);
        Integer id = user.getId();
        userDao.deleteUserIdRole(id);
        if (checkitemIds != null && checkitemIds.length >0){
            for (Integer checkitemId : checkitemIds) {
                userDao.editUserIdRole(id,checkitemId);
            }
        }
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        //删除主表链接
        userDao.deleteUserByRole(id);
        //再删除从表
        userDao.deleteUser(id);

    }


}
